package com.mm.cloud.sys.controller;

import com.mm.cloud.common.core.result.Result;
import com.mm.cloud.common.security.annotation.PreAuthorize;
import com.mm.cloud.sys.dto.form.UserDTO;
import com.mm.cloud.sys.dto.form.UserToRoleDTO;
import com.mm.cloud.sys.dto.query.QueryUserDTO;
import com.mm.cloud.sys.service.IUserService;
import com.mm.cloud.sys.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户基础信息 前端控制器
 * </p>
 *
 * @author mr
 * @since 2020-09-15
 */
@Tag(name = "用户基础信息")
@RestController
@AllArgsConstructor
@RequestMapping("/sys/user")
public class UserController {

    /**
     * 用户基础信息
     */
    private final IUserService userService;

    @Operation(summary = "分页查询【mr】", description = "分页查询")
    @PostMapping("/getUserList")
    @PreAuthorize(hasAuthorize = "ROLE_USER_LIST")
    public Result<List<UserVO>> getUserList(@RequestBody QueryUserDTO dto) {
        return userService.getUserList(dto);
    }

    @Operation(summary = "新增用户【mr】", description = "新增用户")
    @PostMapping("/saveUser")
    @PreAuthorize(hasAuthorize = "ROLE_USER_ADD")
    public Result<String> saveUser(@RequestBody UserDTO dto) {
        return userService.saveUser(dto);
    }

    @Operation(summary = "修改用户【mr】", description = "修改用户")
    @PostMapping("/updateUser")
    @PreAuthorize(hasAuthorize = "ROLE_USER_EDIT")
    public Result<String> updateUser(@RequestBody UserDTO dto) {
        return userService.updateUser(dto);
    }

    @Operation(summary = "删除用户【mr】", description = "删除用户")
    @PostMapping("/deleteUser/{userId}")
    @PreAuthorize(hasAuthorize = "ROLE_USER_DELETE")
    public Result<String> deleteUser(@PathVariable("userId") Long userId) {
        return userService.deleteUser(userId);
    }

    @Operation(summary = "通过用户ID获取角色【mr】", description = "通过用户ID获取角色")
    @PostMapping(value = "/getRoleByUserId/{userId}")
    public Result<List<Long>> getRoleByUserId(@PathVariable("userId") Long userId) {
        return userService.getRoleByUserId(userId);
    }

    @Operation(summary = "用户角色设置【mr】", description = "用户角色设置")
    @PreAuthorize(hasAuthorize = "ROLE_USER_SET")
    @PostMapping(value = "/setUserToRole")
    public Result<String> setUserToRole(@RequestBody UserToRoleDTO dto) {
        return userService.setUserToRole(dto);
    }

}

